package com.example.supermarket.service.impl;

import com.example.supermarket.mapper.BusinessMapper;
import com.example.supermarket.pojo.Business;
import com.example.supermarket.pojo.Item;
import com.example.supermarket.pojo.Order;
import com.example.supermarket.pojo.Shop;
import com.example.supermarket.service.BusinessService;
import com.example.supermarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public void createShop(Shop shop) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        Business business=businessMapper.findByUserId(userId);
        Integer businessId=business.getBusinessId();
        shop.setBusinessId(businessId);
        businessMapper.createShop(shop);
    }

    @Override
    public void createShopItem(Item item) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        Business business=businessMapper.findByUserId(userId);
        Integer businessId=business.getBusinessId();
        item.setBusinessId(businessId);
        businessMapper.createShopItem(item);
    }

    @Override
    public void listItem(Integer shopId, Integer itemId) {
        businessMapper.listItem(shopId,itemId);
        businessMapper.list(itemId);
    }

    @Override
    public void delistItem(Integer shopId, Integer itemId) {
        businessMapper.delistItem(shopId,itemId);
        List<Item> list= businessMapper.findByItemId(itemId);
        if(list.size()==0){
            businessMapper.delist(itemId);
        }
    }

    @Override
    public Business getBusinessInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        return businessMapper.findByUserId(userId);
    }

    @Override
    public List<Order> getOrderByShopId(Integer shopId) {
        return businessMapper.getOrderByShopId(shopId);
    }

    @Override
    public void confirmOrder(Integer orderId) {
        businessMapper.confirmOrder(orderId);
    }

    @Override
    public void cancelOrder(Integer orderId) {
        businessMapper.cancelOrder(orderId);
    }


}
